package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.VO.CategoryListVO;
import cn.tedu.csmall.product.pojo.VO.CategoryStandardVO;
import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/categories")
@Api(tags = "类别管理")
public class CategoryController {
    @Autowired
    ICategoryService categoryService;
    @PostMapping("/add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(CategoryAddNewDTO categoryAddNewDTO){
        log.debug("开始处理添加类别业务");
        categoryService.addNew(categoryAddNewDTO);
        return JsonResult.ok();
    }
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除类别")
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> delete(@Range(min = 1,max = 10000,message = "删除相册失败，相册ID非法！") @PathVariable Long id){
        log.debug("开始处理删除类别的请求，接收到的参数是：{}",id);
        categoryService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改类别")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> update(){
        return null;
    }


    @GetMapping("/{id:[0-9]+}/select")
    @ApiOperation("查询类别详情")
    @ApiOperationSupport(order = 300)
    public JsonResult<CategoryStandardVO> getStandard (@PathVariable Long id){
        log.debug("开始处理查询数据详情的请求，接受到的参数是：{}",id);
        CategoryStandardVO standardById = categoryService.getStandardById(id);
        return JsonResult.ok(standardById);
    }



    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 301)
    public JsonResult<Void> setEnable(@PathVariable Long id){
        log.debug("开始处理修改【启用项】业务请求，修改的对象是：{}",id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 302)
    public JsonResult<Void> setDisable(@PathVariable Long id){
        log.debug("开始处理修改【禁用项】业务请求，修改的对象是：{}",id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/display")
    @ApiOperation("显示导航栏")
    @ApiOperationSupport(order = 301)
    public JsonResult<Void> setDisplay(@PathVariable Long id){
        log.debug("开始处理修改【启用项】业务请求，修改的对象是：{}",id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/doNotDisplay")
    @ApiOperation("不显示导航栏")
    @ApiOperationSupport(order = 302)
    public JsonResult<Void> doNotDisplay(@PathVariable Long id){
        log.debug("开始处理修改【禁用项】业务请求，修改的对象是：{}",id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }



    @GetMapping( "/list-by-parent")
    @ApiOperation("根据父级id查询类别列表")
    @ApiOperationSupport(order = 300)
    public JsonResult<List<CategoryListVO>> listByParentId(Long parentId){
        List<CategoryListVO> lists = categoryService.listByParentId(parentId);
        return JsonResult.ok(lists);
    }
}